//
// Created by g on 2024/6/23.
//

#ifndef FLASH_CNTROL_H
#define FLASH_CNTROL_H

//#include "stdint.h"
//#include "stdio.h"
#include "main.h"
#include "string.h"
#include "w25q128.h"

#define FLASH_PIECE_MIN_SIZE    256
#define FLASH_CONFIG_LEN        4096
#define FLASH_MAX_SIZE          0xFFFFFF
#define FLASH_USEFUL_DATA_LEN   (FLASH_PIECE_MIN_SIZE-8)

#define flash_clear_all 		w25q128_erase_chip
#define flash_clear_piece 	w25q128_erase_sector
#define flash_write_page 		w25q128_write
#define flash_read 					w25q128_read

#define END  0xED
#define JUMP 0xDA

//20: name_H(4) name_L(4) type(4) start_piece(4) file_size(4)
//256: size(1) CMD(1) piece_H(1) piece_L(1)  data(248) 0xFF 0xFE 0xEF 0xFF



#define False 0
#define True  1
#define Bool uint8_t

typedef enum {
    TXT,
		BOOL_CONFIG
}File_Type;

typedef enum {
    Append,
    Rewrite
}Con_type;

typedef enum {
    LEFT,
    RIGHT
}OFFSET_DIRECTION;

typedef struct {
    uint8_t head_start[4];
    uint8_t end_cmd[4];
    uint8_t file_type[2][4];
    uint8_t file_init[10];
    uint8_t empty_file[20];
    uint8_t none_data[248];
    uint8_t lim32_init_if;
}lim32_basic_config_t;

typedef struct{
    uint8_t init_scusses_if;
    uint8_t rewrite_clear_if;
    uint8_t append_init_if;
    uint8_t file[12];
    uint16_t file_id;
    uint32_t now_size;
    uint32_t config_addr;
    uint32_t start_piece;
    uint32_t now_piece;
    uint32_t now_addr;
    uint32_t now_offset;
    Con_type con_type;
}File_operate_t;

extern uint8_t control_buf[FLASH_MAX_SIZE];
extern lim32_basic_config_t lim32_basic_config;

//void flash_print(void);
//void flash_clear_all(void);
//void flash_clear_piece(uint32_t piece_id);
//void flash_write_page(uint8_t* puf,uint32_t addr,uint16_t datalen);
//void flash_read(uint8_t* puf,uint32_t addr,uint16_t datalen);
//void msg_log(uint8_t* msg,uint8_t level);

void flash_init(uint32_t flash_size);
Bool creat_file(uint8_t* file_name,uint8_t file_name_len,File_Type file_type);
Bool del_file(uint8_t* file_name,uint8_t file_name_len,File_Type file_type);
Bool file_size_del(File_operate_t* temp_f,uint32_t del_offset);
Bool file_delete_data(File_operate_t* temp_f,uint32_t del_offset_start,uint32_t del_distances);
Bool open_file(File_operate_t* temp_f,uint8_t* file_name,uint8_t file_name_len,File_Type file_type,Con_type con_type);
Bool file_data_move(File_operate_t* temp_f,uint32_t move_start_offset,uint32_t move_end_offset,uint32_t move_distance,OFFSET_DIRECTION dir);
Bool file_write(File_operate_t* temp_f,uint8_t* datas,uint32_t data_len);
Bool file_write_data(File_operate_t* temp_f,uint8_t* puf,uint32_t offset_start,uint32_t offset);
Bool file_clear(File_operate_t* temp_f);
Bool file_close(File_operate_t* temp_f);
Bool file_size_calu(File_operate_t* temp_f);
Bool file_read_all(File_operate_t* temp_f);
Bool file_read(File_operate_t* temp_f,uint8_t* puf,uint32_t read_offset_start,uint32_t offset);
Bool offset_move(File_operate_t* temp_f,OFFSET_DIRECTION dir,uint32_t distance);

uint32_t get_nums(void);
uint8_t get_piece_size(uint16_t id);
int get_file_id(uint8_t* file,uint32_t file_nums);
Bool in_dir_if(uint8_t* file,uint32_t file_nums);

Bool list_compare(uint8_t* l1,uint8_t* l2,uint32_t len);
uint32_t u8_to_u32(uint8_t* data_u8);
void u32_to_u8(uint32_t data_u32,uint8_t* data_u8);
uint32_t pieceid_to_addr(uint16_t id);
#endif //FLASH_CNTROL_H
